﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO = Viewer.BusinessObject.BusinessEntities;
using Ext.Net;
namespace Viewer.ChucNang.UserControls
{
    public partial class UcChucNang : System.Web.UI.UserControl
    {
        BO.ChucNang chucnang = new Viewer.BusinessObject.BusinessEntities.ChucNang();
        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForControl();
            }
        }
        void LoadDataForControl()
        {
            //Load Tổ chức
            storeChucNang.DataSource = chucnang.LoadAllChucNang();
            storeChucNang.DataBind();

        }
        void ResetControl()
        {

            txtFullName.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtShortName.Text = string.Empty;
            ccbToChucCha.Text = string.Empty;
        }
        public void ShowDialog(string type, int id, int idToChucCha)
        {
            //AddNew
            wdChucNang.Show();
            if (type.Equals("AddNew"))
            {
                ResetControl();
                wdChucNang.Title = "Tạo mới chức năng";
                hdStatus.Value = "AddNew";
                hdIdCha.Value = idToChucCha.ToString();
                btnSave.Hidden = false;
                txtGhiChu.ReadOnly = false;
                txtFullName.ReadOnly = false;
                txtShortName.ReadOnly = false;
                ccbToChucCha.SelectedItem.Value = idToChucCha > 0 ? idToChucCha.ToString() : "Không có tổ chức cha";

            }
            else if (type.Equals("View"))
            {
                wdChucNang.Title = "Chi tiết chức năng";
                btnSave.Hidden = true;
                ccbToChucCha.ReadOnly = true;
                txtGhiChu.ReadOnly = true;
                txtFullName.ReadOnly = true;
                txtShortName.ReadOnly = true;
                LoadDataView(id);
            }
            else if (type.Equals("Edit"))
            {
                hdStatus.Value = "Edit";
                hdIdCha.Value = idToChucCha.ToString();
                btnSave.Hidden = false;
                txtGhiChu.ReadOnly = false;
                txtFullName.ReadOnly = false;
                txtShortName.ReadOnly = false;
                wdChucNang.Title = "Sửa chức năng";
                ccbToChucCha.ReadOnly = true;
                LoadDataView(id);
            }

        }
        void LoadDataView(int iD)
        {
            List<string> query = chucnang.LoadChucNangById(iD);
            txtId.Text = query[0];
            txtFullName.Text = query[1];
            txtShortName.Text = query[2];
            ccbToChucCha.SelectedItem.Value = query[3] != string.Empty && Convert.ToInt16(query[3]) > 0 ? query[3] : "Không có tổ chức cha";
            txtGhiChu.Text = query[5];
            txtLink.Text = query[6];
            txtIconName.Text = query[7];
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            int idCha = (hdIdCha.Text != "" ? int.Parse(hdIdCha.Text) : 0);
            if (hdStatus.Value.ToString() == "AddNew")
            {
                chucnang.InsertChucNang(txtShortName.Text, txtFullName.Text, idCha, 2,txtGhiChu.Text,txtLink.Text,txtIconName.Text );
                ((PhanQuyenChucNang)this.Page).LoadDataForTree();
                ((PhanQuyenChucNang)this.Page).LoadGrid(idCha.ToString(),"");
                wdChucNang.Hide();
            }
            else
            {
                chucnang.UpdateChucNang(Convert.ToInt16(txtId.Text), txtShortName.Text, txtFullName.Text, txtGhiChu.Text, txtLink.Text, txtIconName.Text);
                ((PhanQuyenChucNang)this.Page).LoadGrid(idCha.ToString(),"");
                ((PhanQuyenChucNang)this.Page).LoadDataForTree();
                wdChucNang.Hide();
            }
        }
    }
}